// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj

const {
	rejects
} = require("assert")

// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database()
const user = db.collection('user')
module.exports = {
	_before: function() { // 通用预处理器

	},
	async login(data) {
		const res = await user.where(data).get()
		console.log(res);
		if (res.affectedDocs > 0) {
			// 找到此账号
			return {
				code: 200,
				message: '登录成功',
				_id: res.data[0]._id,
				token: "9152615614" + new Date()
			}
		} else {
			return {
				code: 201,
				message: '账号或密码错误',
				token: ""
			}
		}
	},
	async signIn(data) {
		if (!data.account || !data.password) {
			return {
				errSubject: '账号或密码不能为空',
				errCode: 0,
				errMsg: '账号或密码不能为空'
			}
		}
		const res = await user.where({
			account: data.account
		}).get()
		if (res.affectedDocs > 0) {
			// 找到此账号
			return {
				code: 201,
				message: '此账号已注册',
				token: ""
			}
		} else {
			const res = await user.add(data)
			return {
				code: 200,
				message: '注册成功',
				_id: res.id,
				token: "9152615614" + new Date()
			}
		}

	},



}